﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FastData
{
    public class VARBase
    {
        protected string address = "";
        public string Address
        {
            get
            {
                return address;
            }
            set
            {
                address = value;
            }
        }
    }



    public class VarSave<T> : VARBase
    {
        private dynamic m_value;
        public T Value
        {
            get
            {

                //m_value = (T)FastData.SaveStatic.ReadBinF(address);
                return m_value;
            }
            set
            {
                if (address != "" && !FastData.DesignHelper.IsDesignMode)
                {
                    FastData.SaveStatic.SaveBinF(address, value);
                }
                m_value = value;
            }
        }

        public VarSave(string _address)
        {
            Address = _address;
            if (!FastData.SaveStatic.ExistBinF(_address))
            {
                string typeName = typeof(T).Name;
                switch (typeName)
                {
                    case "Int32":
                        m_value = 0;
                        break;
                    case "Int64":
                        m_value = 0;
                        break;
                    case "Single":
                        m_value = 0f;
                        break;
                    case "Double":
                        m_value = 0d;
                        break;
                    case "Boolean":
                        m_value = false;
                        break;
                    case "String":
                        m_value = "null1";
                        break;
                    default:
                        m_value = (T)Activator.CreateInstance(typeof(T));
                        break;
                }
                FastData.SaveStatic.SaveBinF(_address, m_value);
            }
            else
            {
                m_value = (T)FastData.SaveStatic.ReadBinF(address);
            }
        }
    }


    public class VarSaveXML<T> : VARBase
    {
        private dynamic m_value;
        public T Value
        {
            get
            {
                //m_value = (T)FastData.SaveStatic.ReadBinF(address);
                return m_value;
            }
            set
            {
                FastData.SaveStatic.SaveXMLF(address, value);
                m_value = value;
            }
        }

        public VarSaveXML(string _address)
        {
            Address = _address;
            if (!FastData.SaveStatic.ExistXMLF(_address))
            {
                string typeName = typeof(T).Name;
                switch (typeName)
                {
                    case "Int16":
                        m_value = (Int16)0;
                        break;
                    case "Int32":
                        m_value = (Int32)0;
                        break;
                    case "Int64":
                        m_value = ((Int64)0);
                        break;
                    case "Single":
                        m_value = 0f;
                        break;
                    case "Double":
                        m_value = 0d;
                        break;
                    case "Boolean":
                        m_value = false;
                        break;
                    case "String":
                        m_value = "null1";
                        break;
                    default:
                        m_value = (T)Activator.CreateInstance(typeof(T));
                        break;
                }
                FastData.SaveStatic.SaveXMLF(_address, m_value);
            }
            else
            {
                m_value = (T)FastData.SaveStatic.ReadXMLF<T>(address);
            }
        }
    }

}
